//
//  WPOrderDetailAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/10.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPOrderDetailAlertView.h"

@interface WPOrderDetailAlertView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSString * orderInfo;/**< 订单信息 */
@property (nonatomic, strong) NSString * payCount;/**< 应付金额 */
@property (nonatomic, strong) UITableView * orderTabaleView;/**< 订单详情列表 */
@property (nonatomic, strong) NSArray * orderTitls;/**< 订单信息标题 */
@property (nonatomic, strong) UIButton * payBtn;/**< 支付按钮 */
@property (nonatomic, copy) void (^complete)(NSString *result);/**< 结果block */
- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeAppearance;/**< 一般界面初始化 */
@end
@implementation WPOrderDetailAlertView
+ (WPOrderDetailAlertView *)showViewWithSize:(CGRect)rect andWithTitle:(NSString *)title andWithOrderInfo:(NSString *)orderInfo andWithOrderMoenyCount:(NSString *)payCount WithComplete:(void (^)(NSString *))complete {
    WPOrderDetailAlertView * view = [[WPOrderDetailAlertView alloc]initWithTitle:title withSize:rect andWithOrderInfo:orderInfo andWithOrderMoenyCount:payCount WithComplete:complete];
    [view show];
    return view;
}

- (instancetype)initWithTitle:(NSString *)title withSize:(CGRect)rect andWithOrderInfo:(NSString *)orderInfo andWithOrderMoenyCount:(NSString *)payCount WithComplete:(void (^)(NSString *))complete {
    self = [super initWithFrame:rect withTitle:title];
    if (self) {
        _orderInfo = orderInfo;
        _payCount = payCount;
        _complete = complete;
        [self initializeDataSource];
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeDataSource {
    self.orderTitls = @[@"订单信息", @"付款方式", @"需付款"];
}
- (void)initializeAppearance {
    [self addSubview:self.orderTabaleView];
    [self addSubview:self.payBtn];
}
#pragma mark - responds events
- (void)respondsToPayBtn {
    [self hide];
    _complete(@"付款");
}
#pragma mark - 协议UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _orderTitls.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"123"];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];

    cell.textLabel.text = _orderTitls[indexPath.row];
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = _orderInfo;
        cell.textLabel.textColor = [UIColor grayColor];
    }
    if (indexPath.row == 1) {
        cell.detailTextLabel.text = @"账户余额";
        cell.textLabel.textColor = [UIColor grayColor];
    }
    if (indexPath.row == 2) {
        cell.detailTextLabel.text = _payCount;
        cell.detailTextLabel.textColor = [UIColor blackColor];
    }
    return cell;
}
#pragma mark - UITableViewDelegate
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40*DHFlexibleHorizontalMutiplier();
}
#pragma mark - getter
- (UITableView *)orderTabaleView {
    if (!_orderTabaleView) {
        _orderTabaleView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier()+2, 250*DHFlexibleHorizontalMutiplier(), CGRectGetHeight(self.bounds)-90*DHFlexibleHorizontalMutiplier()-2) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableFooterView = [UIView new];
            tableview.pagingEnabled = NO;
            tableview.bounces = NO;
            tableview.scrollEnabled = NO;
            tableview.separatorColor = COLOR_RGB(211, 211, 211, 1);
            tableview;
        });
    }
    return _orderTabaleView;
}
- (UIButton *)payBtn {
    if (!_payBtn) {
        _payBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(50*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.bounds) - 40*DHFlexibleHorizontalMutiplier(), 170*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"确认付款" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [btn addTarget:self action:@selector(respondsToPayBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
            
        });
    }
    return _payBtn;
}

@end
